package com.twotiger.and.util;

import java.io.File;
import java.util.HashMap;

import com.alibaba.fastjson.JSONObject;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * 
 * @Description: 
 * 系统工具类
 * @Author douxy
 * @Date 2015年7月9日 下午1:33:13
 */
public class Tools {
    /**
     * 检查是否存在SDCard
     *
     * @return
     */
    public static boolean hasSdcard() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取 deviceId
     *
     * @return
     */
    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }

    /**
     * 获取 user-agent
     * 
     * @param context
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getUserAgent(Context context) {

        //useragent 示例：TwoTiger/2.5.1 (HM 2A; Android 4.4.4; API/19) Channel/tiger NetType/WIFI Pixel/1280*720

        return String.format("TwoTiger/%s (" + android.os.Build.MODEL + "; Android "
                             + android.os.Build.VERSION.RELEASE + "; API/"
                             + android.os.Build.VERSION.SDK + ")"
                             + " Channel/%s NetType/%s Pixel/%s",
            PackageUtils.getAppVersionName(context),
            PackageUtils.getAppMetaData(context, "UMENG_CHANNEL"),
            NetWorkUtil.getNetTypeName(context), ScreenUtils.getScreenPixel(context));
    }

    /**
     * 获取手机型号等信息
     * 
     * @return
     */
    public static String getMobileInfo() {
        return "手机型号: " + android.os.Build.MODEL + ",SDK版本: " + android.os.Build.VERSION.SDK
               + ",系统版本: " + android.os.Build.VERSION.RELEASE + "------";
    }

    public static long getSDFreeSize() {
        // 取得SD卡文件路径
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        // 获取单个数据块的大小(Byte)
        long blockSize = sf.getBlockSizeLong();
        // 空闲的数据块的数量
        long freeBlocks = sf.getAvailableBlocksLong();
        // 返回SD卡空闲大小
        return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB
    }

    /**
     * 友盟设备识别信息输出
     * 
     * @param context
     * @return
     */
    public static String getDeviceInfo(Context context) {
        try {
            org.json.JSONObject json = new org.json.JSONObject();
            android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

            String device_id = tm.getDeviceId();

            android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);

            String mac = wifi.getConnectionInfo().getMacAddress();
            json.put("mac", mac);

            if (TextUtils.isEmpty(device_id)) {
                device_id = mac;
            }

            if (TextUtils.isEmpty(device_id)) {
                device_id = android.provider.Settings.Secure.getString(
                    context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
            }

            json.put("device_id", device_id);

            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
